home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ftp / RCS / pclose.c,v < prev    next >
Encoding:
Text File  |  1990-10-27  |  2.4 KB  |  123 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    shirriff:1.1; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.10.27.13.45.50;  author shirriff;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1980 Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that this notice is preserved and that due credit is given
  31.  * to the University of California at Berkeley. The name of the University
  32.  * may not be used to endorse or promote products derived from this
  33.  * software without specific prior written permission. This software
  34.  * is provided ``as is'' without express or implied warranty.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@@(#)pclose.c    1.3 (Berkeley) 3/14/88";
  39. #endif /* not lint */
  40.  
  41. #include <stdio.h>
  42. #include <signal.h>
  43. #include <sys/param.h>
  44. #include <sys/wait.h>
  45.  
  46. #define    tst(a,b)    (*mode == 'r'? (b) : (a))
  47. #define    RDR    0
  48. #define    WTR    1
  49.  
  50. extern    char *malloc();
  51.  
  52. static    int *popen_pid;
  53. static    int nfiles;
  54.  
  55. FILE *
  56. mypopen(cmd,mode)
  57.     char *cmd;
  58.     char *mode;
  59. {
  60.     int p[2];
  61.     int myside, hisside, pid;
  62.  
  63.     if (nfiles <= 0)
  64.         nfiles = getdtablesize();
  65.     if (popen_pid == NULL) {
  66.         popen_pid = (int *)malloc((unsigned) nfiles * sizeof *popen_pid);
  67.         if (popen_pid == NULL)
  68.             return (NULL);
  69.         for (pid = 0; pid < nfiles; pid++)
  70.             popen_pid[pid] = -1;
  71.     }
  72.     if (pipe(p) < 0)
  73.         return (NULL);
  74.     myside = tst(p[WTR], p[RDR]);
  75.     hisside = tst(p[RDR], p[WTR]);
  76.     if ((pid = vfork()) == 0) {
  77.         /* myside and hisside reverse roles in child */
  78.         (void) close(myside);
  79.         if (hisside != tst(0, 1)) {
  80.             (void) dup2(hisside, tst(0, 1));
  81.             (void) close(hisside);
  82.         }
  83.         execl("/bin/sh", "sh", "-c", cmd, (char *)NULL);
  84.         _exit(127);
  85.     }
  86.     if (pid == -1) {
  87.         (void) close(myside);
  88.         (void) close(hisside);
  89.         return (NULL);
  90.     }
  91.     popen_pid[myside] = pid;
  92.     (void) close(hisside);
  93.     return (fdopen(myside, mode));
  94. }
  95.  
  96. pabort()
  97. {
  98.     extern int mflag;
  99.  
  100.     mflag = 0;
  101. }
  102.  
  103. mypclose(ptr)
  104.     FILE *ptr;
  105. {
  106.     int child, pid, omask, pabort(), (*istat)();
  107.     union wait status;
  108.  
  109.     child = popen_pid[fileno(ptr)];
  110.     popen_pid[fileno(ptr)] = -1;
  111.     (void) fclose(ptr);
  112.     if (child == -1)
  113.         return (-1);
  114.     istat = signal(SIGINT, pabort);
  115.     omask = sigblock(sigmask(SIGQUIT)|sigmask(SIGHUP));
  116.     while ((pid = wait(&status)) != child && pid != -1)
  117.         ;
  118.     (void) sigsetmask(omask);
  119.     (void) signal(SIGINT, istat);
  120.     return (pid == -1 ? -1 : 0);
  121. }
  122. @
  123.